package com.boarsoft.boar.batch.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.batch.bean.BatchAppStat;
import com.boarsoft.boar.batch.service.BatchBrokerService;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.entity.AppInst;
import com.boarsoft.boar.service.AppInstService;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;

@Deprecated
@RestController
@RequestMapping("/app")
public class BatchAppAction {
	private static final Logger log = LoggerFactory.getLogger(BatchAppAction.class);

	@Autowired(required = false)
	private AppInstService appInstService;

	@Autowired
	private ApplicationContext applicationContext;

	
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@Authorized(code = "batch.app.list")
	public ReplyInfo<String> list(String broker, String catalog, String appId, String env, int pageSize) {
		if (Util.strIsEmpty(broker)) {
			return new ReplyInfo<String>(Constants.WARN_INVALID, "broker");
		}
		if (Util.strIsEmpty(catalog)) {
			return new ReplyInfo<String>(Constants.WARN_INVALID, "catalog");
		}
		if (!applicationContext.containsBean(broker)) {
			return new ReplyInfo<String>(Constants.ERROR_NOTFOUND, "broker");
		}
		// 获取对应的BatchBrokerService
		BatchBrokerService bbs = applicationContext.getBean(broker, BatchBrokerService.class);
		// 广播调用所有批量实例
		Map<String, Object> rm = (Map<String, Object>) bbs.stat();
		for (Entry<String, Object> en : rm.entrySet()) {
			String k = en.getKey();
			Object o = en.getValue();
			BatchAppStat s = null;
			if (o == null) {
				log.error("Can not get stat from {}", k);
				s = new BatchAppStat(catalog, k, AppInst.STATUS_UNKNOWN);
			} else if (o instanceof Throwable) {
				log.error("Error on get stat from {}", k, o);
				s = new BatchAppStat(catalog, k, AppInst.STATUS_FAILED);
			}
			if (o instanceof BatchAppStat) {
				s = (BatchAppStat) o;
				s.setStatus(AppInst.STATUS_RUNNING);
			}
			rm.put(k, s);
		}
		// 遍历所有指定应用的实例
		if (appInstService != null) {
			List<AppInst> lt = appInstService.list(appId, env, null, null);
			for (AppInst a : lt) {
				String k = a.getAddr();
				if (rm.containsKey(k)) {
					continue;
				}
				rm.put(k, new BatchAppStat(catalog, k, AppInst.STATUS_UNKNOWN));
			}
		}
		List<BatchAppStat> lt = new ArrayList<BatchAppStat>();
		for (Object s : rm.values()) {
			lt.add((BatchAppStat) s);
		}
		PagedResult<BatchAppStat> pr = //
				new PagedResult<BatchAppStat>(lt.size(), lt, 1, pageSize);
		return new ReplyInfo<String>(true, JsonUtil.from(pr));
	}

	@RequestMapping("/delete")
	@Authorized(code = "batch.app.delete")
	public ReplyInfo<Object> delete(String addr) {
		if (Util.strIsEmpty(addr)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, "addr");
		}
		if (appInstService == null) {
			return new ReplyInfo<Object>(Constants.ERROR_UNSUPPORTED);
		}
		try {
			appInstService.delete(addr);
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error on delete batch inst", e);
			return ReplyInfo.FAILED;
		}
	}
}